package com.chensk.study.netty.firstexample;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.bootstrap.ServerBootstrapConfig;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Server {
    public static void main( String[] args ) {
        //用于处理连接的事件循环组
        EventLoopGroup parentGroup=new NioEventLoopGroup();
        //用于处理消息的事件循环组
        EventLoopGroup childGroup=new NioEventLoopGroup();
        ServerBootstrap serverBootstrap=new ServerBootstrap();
        try {
            ChannelFuture channelFuture= serverBootstrap.group(parentGroup,childGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelHandler(){
                public void handlerAdded( ChannelHandlerContext ctx ) throws Exception {
                    System.out.println("有新的连接进来");
                }

                public void handlerRemoved( ChannelHandlerContext ctx ) throws Exception {
                    System.out.println("有新的连接出去");
                }

                public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) throws Exception {
                    System.out.println("抛出异常");
                }
            } ).bind(6666).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }


    }
}
